@(pageNode: spray.site.ContentNode)

@import spray.site._

<ul class="nav">

  <li><a href="/">Home</a></li>

  @anchor(node: ContentNode, caret: Boolean) = {
    <a href="@node.absoluteUri">@node.name@if(caret){<b class="caret"></b>}</a>
  }

  @nextLevel(node: ContentNode) = {
    <ul class="dropdown-menu">
      @node.children.map(item(false))
    </ul>
  }

  @classes(node: ContentNode, first: String = null) = @{
    List(
      Option(first).toList,
      if (node.isLast) "last" :: Nil else Nil,
      if (pageNode.isDescendantOf(node)) "active" :: Nil else Nil
    ).flatten match {
      case Nil => ""
      case x => x.mkString(" class=\"", " ", "\"")
    }
  }

  @item(goDown: Boolean)(node: ContentNode) = {
    @if(goDown && !node.isLeaf) {
      <li@HtmlFormat.raw(classes(node, "dropdown"))>@anchor(node, true) @nextLevel(node)</li>
    } else {
      <li@HtmlFormat.raw(classes(node))>@anchor(node, false)</li>
    }
  }

  @Main.root.children.map(item(true))

</ul>